﻿using DTOs;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain;
using RBAC.Domain.Service.Impl;
using RBAC.Domain.Service.Interfae;
using RBACDuYaJun.Api.Appliction.Command.RoleCommand;
using RBACDuYaJun.Api.Dtos;
using System.Security.Claims;

namespace RBACDuYaJun.Api.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]//（JWT）
    [ApiController]
    public class RolesController : ControllerBase
    {
        /// <summary>
        /// 引入IMediator
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 角色服务
        /// </summary>
        protected readonly IRoleService _roleService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService"></param>
        public RolesController(IRoleService roleService,IMediator mediator)
        {
            _roleService = roleService;
            _mediator = mediator;
        }
        /// <summary>
        /// 反填（从Token中取角色ID）1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Roles>> GetRoleAlls()
        {
            ApiResult<Roles> result = new ApiResult<Roles>();
            try
            {
                var rid = Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.Role)?.Value);//获取sub的值
                result.Data = await _roleService.GetRoleAlls(rid);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
        }
        /// <summary>
        /// 利用IMediator进行添加角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddRoles(RoleCreateCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        ///逻辑删除角色（利用IMediator）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DelRole([FromQuery]RoleDeleteCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 显示角色（利用IMediator）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<Roles>>> GetRoleAsync([FromQuery]RoleShowCommand command)
        {
            ApiResult<PageDto<Roles>> result = new ApiResult<PageDto<Roles>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
            // return await _roleService.GetRoleAsync(pageSize, pageIndex);
            // return await _mediator.Send(new RoleShowCommand());
        }
        /// <summary>
        /// 反填（利用IMediator）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Roles>> GetRoleByValues([FromQuery]RoleGetIdCommand command)
        {
            ApiResult<Roles> result = new ApiResult<Roles>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
            //return await _mediator.Send(command);
        }
        /// <summary>
        /// 修改角色信息（利用IMediator）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateRole(RoleUpdateCommand command)
        {
            return await _mediator.Send(command);
        }
        
        /// <summary>
        /// 查询部门下的员工（级联）
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Menument>> GetUserRole(List<int> ids)
        {
            return await _roleService.GetUserRole(ids);
        }
    }
}
